package geral;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class JanelaIntegral extends JFrame{
	private JTextField btnFunc;
	private JTextField btnMin;
	private JTextField btnMax;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel lblInsiraONmero;
	private JTextField btnDivisoes;
	private Integravel i;

	public JanelaIntegral(Integravel i){
		this.i = i;
		getContentPane().setLayout(null);
		
				
		btnFunc = new JTextField();
		btnFunc.setBounds(10, 39, 179, 20);
		getContentPane().add(btnFunc);
		btnFunc.setColumns(10);
		
		JLabel lblInsiraAFuno = new JLabel("Insira a fun\u00E7\u00E3o:");
		lblInsiraAFuno.setBounds(50, 22, 180, 14);
		getContentPane().add(lblInsiraAFuno);
		
		JLabel lblInsiraOsIntervalos = new JLabel("Insira os intervalos:");
		lblInsiraOsIntervalos.setBounds(35, 70, 200, 14);
		getContentPane().add(lblInsiraOsIntervalos);
		
		btnMin = new JTextField();
		btnMin.setBounds(49, 95, 30, 20);
		getContentPane().add(btnMin);
		btnMin.setColumns(10);
		
		btnMax = new JTextField();
		btnMax.setColumns(10);
		btnMax.setBounds(89, 95, 30, 20);
		getContentPane().add(btnMax);
		
		JLabel label = new JLabel(";");
		label.setBounds(83, 98, 10, 14);
		getContentPane().add(label);
		
		label_1 = new JLabel("[");
		label_1.setBounds(40, 97, 10, 14);
		getContentPane().add(label_1);
		
		label_2 = new JLabel("]");
		label_2.setBounds(122, 97, 10, 14);
		getContentPane().add(label_2);
		
		lblInsiraONmero = new JLabel("Insira o n\u00FAmero de divis\u00F5es:");
		lblInsiraONmero.setBounds(10, 133, 200, 14);
		getContentPane().add(lblInsiraONmero);
		
		btnDivisoes = new JTextField();
		btnDivisoes.setBounds(76, 158, 40, 20);
		getContentPane().add(btnDivisoes);
		btnDivisoes.setColumns(10);
		
		JButton btnSubmeter = new JButton("Submeter");
		btnSubmeter.setBounds(50, 196, 110, 23);
		getContentPane().add(btnSubmeter);
		//btnSubmeter.addActionListener(new btnListener(i, this.btnFunc, this.btnMin, this.btnMax, this.btnDivisoes));
		btnSubmeter.addActionListener(new btnListener(i, this));
		
		setupLookAndFeel();
		this.setSize(new Dimension(218, 300));
		this.setTitle("Teste");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	private class btnListener implements ActionListener{
		private Integravel i;
		private JTextField funcao, min, max, divisoes;
		private JanelaIntegral f;
		
		public btnListener(Integravel i, JanelaIntegral f){
			this.i = i;
			this.funcao = f.getBtnFunc();
			this.min = f.getBtnMin();
			this.max = f.getBtnMax();
			this.divisoes = f.getBtnDivisoes();
			this.f = f;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			i.setFuncao(new Funcao(funcao.getText()));
			i.setMin(Integer.parseInt(min.getText()));
			i.setMax(Integer.parseInt(max.getText()));
			i.setDivisoes(Integer.parseInt(divisoes.getText()));
			//f.fechar();
			JOptionPane.showMessageDialog(null, "A área da função é: \n"+i.calcula());
		}
		
	}
	
	
	public void setupLookAndFeel(){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}
	
	public JTextField getBtnFunc() {
		return btnFunc;
	}


	public JTextField getBtnMin() {
		return btnMin;
	}


	public JTextField getBtnMax() {
		return btnMax;
	}


	public JTextField getBtnDivisoes() {
		return btnDivisoes;
	}
	
	public void fechar(){
		this.setVisible(false);
	}
	
}